<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 单位ajax类
 * @package  	ajax
 * @subpackage  backend
 * @author   	allen.qiang
 */

quanli_require('module/base/goodsHandler.class.php');

class ajax_unit {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //查询商品单位
                    $this->searchUnit();
                    break;
			    case 2: //添加和修改商品单位
                    $this->saveUnit();
                    break;
				case 4: //删除商品单位
					$this->deleteUnit();
					break;
				case 5: //商品单位使用标志 
					$this->actionUnit();
					break;

                case 6: //待编辑商品单位查询
                	$this->searchOneUnit();
                break;

                case 7: //商品单位排序序号查询
                    $this->searchsort();
                    break;
            }

        }

    }

    /**
     * 查询商品单位
     */
    private function searchUnit() {

        global $G_PGLOB;
        
        //参数
        $sortname  = $_REQUEST['sortname'] ;
        $sortorder = $_REQUEST['sortorder'] ;
        $page	   = $_REQUEST['page'] ;
        $rp		   = $_REQUEST['rp'] ;
        $unitName  = trim($_REQUEST['unitName']) ;
        
        //查询条件
		$arg_array = array(
			'sortname' 	=> $sortname , 
			'sortorder' => $sortorder , 
			'page' 		=> $page , 
			'rp' 		=> $rp 
		);

		//字段查询方式
		$extend_array = array() ;
		if( $unitName != '' ) {
			$arg_array['unitName'] 	   = $unitName ;
			$extend_array ['unitName'] = 1  ; 
		}
		
		$commHandler = new commHandler( $G_PGLOB->DBCA ) ;
		$result_data = $commHandler->search( $arg_array , 'goods_unit' , $extend_array ) ;
		
		$result_array = array(
            'page'  => $arg_array['page'] ,
            'total' => $arg_array['count'] ,
            'rows'  => array()
        );
        
		if ($result_data) {
			$edit_permission = $G_PGLOB->has_permission(506);
            foreach($result_data as &$d_l) {
            	if($edit_permission){
	                $result_array['rows'][] = array(
	                    'id' 	=> $d_l->unitID,
	                    'cell' 	=> array(
	            			'<a href="javascript: void(0);" onclick="unit.edit(' . $d_l->unitID . ')">修改</a>&nbsp;' ,
	            			$d_l->unitID ,
	            			$d_l->unitName,
	            			$d_l->sortOrder,
	            			$d_l->useFlg == 1 ?
	            			'<a href="javascript: void(0);" onclick="unit.doAction(' . $d_l->unitID . ', 0) " >启用</a>' :
	            			'<a href="javascript: void(0);" onclick="unit.doAction(' . $d_l->unitID . ', 1 ) " >禁用</a>' 
	                    ),
	                );
            	}else{
            		$result_array['rows'][] = array(
	                    'id' 	=> $d_l->unitID,
	                    'cell' 	=> array(
                			$d_l->unitID ,
                			$d_l->unitName,
                			$d_l->sortOrder,
                			$d_l->useFlg == 1 ?
                			'启用' :
                			'禁用'
	                    ),
	                );
            	}
            }
        }

        echo json_encode( $result_array );
    }
    /**
     * 单位保存和更新
     *
     */
    private function saveUnit () {
    	global $G_PGLOB;
    	
    	$_REQUEST['creator'] = $G_PGLOB->currentUserID;
    	$goodsHandler = new goodsHandler( $G_PGLOB->DBCA ) ;

    	$result_array = $goodsHandler->saveUnitInfo( $_REQUEST ) ;

        $result_array['sort'] = ++$_REQUEST['sortOrder'];

    	echo json_encode($result_array);
    }
	/**
	 * 删除单位
	 *
	 */
	private function deleteUnit () {
		global $G_PGLOB;
		
    	$goodsHandler = new goodsHandler( $G_PGLOB->DBCA ) ;
    	$result_array = $goodsHandler->deleteUnit( $_REQUEST ) ;
    	echo json_encode($result_array);
	}
	/**
	 * 修改单位的使用状态
	 *
	 */
	private function actionUnit () {
		global $G_PGLOB;
		
    	$goodsHandler = new goodsHandler( $G_PGLOB->DBCA ) ;
    	$result_array = $goodsHandler->actionUnit( $_REQUEST ) ;
    	echo json_encode($result_array);
	}


    //查询待编辑信息
    private function searchOneUnit() {

        global $G_PGLOB;

        //参数
        $unitID  = $_REQUEST['unitID'] ;

        //查询条件
        $arg_array = array(
            'unitID' 	=> $unitID
        );

        $commHandler = new commHandler( $G_PGLOB->DBCA ) ;
        $result_data = $commHandler->search( $arg_array , 'goods_unit'  ) ;

        $result = array(
            'unitName' => $result_data[0]->unitName,
            'sortOrder' => $result_data[0]->sortOrder,
            'useFlg' => $result_data[0]->useFlg,
            'unitID' => $result_data[0]->unitID
        );

        echo json_encode( $result );
    }

    //查询排序序号
    private function searchsort(){

        global $G_PGLOB;
        //实例化系统公共类获取最大序列号
        $commHandler = new commHandler( $G_PGLOB->DBCA );
        $sort = $commHandler->getSortNum("goods_unit","sortOrder");
        $result = array('sortOrder'=>$sort);
        echo json_encode($result);

    }
}
